//
//  LoginViewController.m
//  Mango
//
//  Created by lanou3g on 16/5/16.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#define LoginUrl @"http://api2.pianke.me/user/login"

#import "LoginViewController.h"
#import "RRegisterViewController.h"
#import "XJUserInfoManager.h"
#import <SVProgressHUD.h>

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *emailField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (nonatomic, copy) NSString *currentSkinModel;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.passwordField.textColor = [UIColor whiteColor];
    self.emailField.textColor = [UIColor whiteColor];
}


- (IBAction)closeAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)loginAction:(UIButton *)sender {
    NSDictionary *paraDict = @{@"email":self.emailField.text,@"passwd":self.passwordField.text};
    [[AFHTTPSessionManager manager] POST:LoginUrl parameters:paraDict progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dataDict = responseObject[@"data"];
        NSString *msg = dataDict[@"msg"];
        msg = msg ? msg : @"登录成功";
        
        NSInteger result = [responseObject[@"result"] integerValue];
        if (result == 1) {          //登录成功
            //存储用户信息
            [XJUserInfoManager shareManager].loginStatus = YES;
            [[XJUserInfoManager shareManager] loginWithDict:dataDict];
            [[XJUserInfoManager shareManager]saveUserInfo];
            [self dismissViewControllerAnimated:YES completion:nil];
        }else {
            [SVProgressHUD showInfoWithStatus:msg];
            [self performSelector:@selector(dismissShowView) withObject:nil afterDelay:1];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"%@",error);
    }];
}

- (void)dismissShowView {
    [SVProgressHUD dismiss];
}

- (IBAction)registerAction:(UIButton *)sender {
    RRegisterViewController *registerVC = [[RRegisterViewController alloc] init];
    [self presentViewController:registerVC animated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
